package com.brillilab.service.core.experiments.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.brillilab.dao.mapper.experiments.PlateGridTextMapper;
import com.brillilab.dao.mapper.experiments.PlateMapper;
import com.brillilab.domain.enums.common.BoolEnum;
import com.brillilab.domain.po.experiments.Plate;
import com.brillilab.domain.po.experiments.PlateGridText;
import com.brillilab.domain.vo.experiments.PlateGridTextVo;
import com.brillilab.service.core.experiments.IPlateGridTextService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@Service
@Transactional
public class PlateGridTextServiceImpl implements IPlateGridTextService {

    @Resource
    private PlateGridTextMapper plateGridTextMapper;
    @Resource
    private PlateMapper plateMapper;

    @Override
    public boolean insert(PlateGridText gridText) {
        return plateGridTextMapper.insert(gridText) > 0;
    }

    @Override
    public int selectCountOnePlate(Long plateId) {
        return plateGridTextMapper.selectCount(
                new LambdaQueryWrapper<PlateGridText>()
                        .eq(PlateGridText::getPlateId,plateId));
    }

    @Override
    public PlateGridText selectOne(Long plateId,Long gridId,Long stepTextId) {
        return plateGridTextMapper.selectOne(
                new LambdaQueryWrapper<PlateGridText>()
                        .eq(PlateGridText::getPlateId,plateId)
                        .eq(PlateGridText::getPlateGridId,gridId)
                        .eq(PlateGridText::getStepTextId,stepTextId));
    }

    @Override
    public List<PlateGridText> selectList(Long plateId) {
        return plateGridTextMapper.selectList(new LambdaQueryWrapper<PlateGridText>().eq(PlateGridText::getPlateId,plateId));
    }

    @Override
    public List<PlateGridTextVo> selectVoList(Long plateId,Long gridId) {
        return plateGridTextMapper.selectVoList(plateId,gridId);
    }

    @Override
    public boolean deleteByPlateId(Long plateId) {
        return plateGridTextMapper.delete(
                new LambdaQueryWrapper<PlateGridText>()
                        .eq(PlateGridText::getPlateId,plateId)) > 0;
    }

    @Override
    public PlateGridText selectOne(Long stepTextId,Long gridId) {
        return plateGridTextMapper.selectOne(
                new LambdaQueryWrapper<PlateGridText>()
                        .eq(PlateGridText::getPlateGridId,gridId)
                        .eq(PlateGridText::getStepTextId,stepTextId));
    }

    @Override
    public boolean deleteById(Long id) {
        return plateGridTextMapper.deleteById(id) > 0;
    }

    @Override
    public int selectCountOneGrid(Long gridId) {
        return  plateGridTextMapper.selectCount(
                new LambdaQueryWrapper<PlateGridText>()
                        .eq(PlateGridText::getPlateGridId,gridId));
    }

    @Override
    public int deleteInGridIds(Long stepTextId,List<Long> gridIds) {
        if(gridIds.size()>0){
            return plateGridTextMapper.delete(
                    new LambdaQueryWrapper<PlateGridText>()
                            .eq(PlateGridText::getStepTextId,stepTextId)
                            .in(PlateGridText::getPlateGridId,gridIds));
        }
        return 0;
    }

    @Override
    public List<PlateGridText> selectList(List<Long> gridIds) {
        if(gridIds.size()>0){
            return plateGridTextMapper.selectList(
                    new LambdaQueryWrapper<PlateGridText>()
                            .in(PlateGridText::getPlateGridId,gridIds));
        }
        return new ArrayList<>();
    }

    @Override
    public int insertBatch(List<PlateGridText> texts) {
        if(texts.size()>0){
            return plateGridTextMapper.insertBatch(texts);
        }
        return 0;
    }

    @Override
    public int deleteByStepTextId(Long textId) {
        return plateGridTextMapper.delete(
                new LambdaQueryWrapper<PlateGridText>()
                        .eq(PlateGridText::getStepTextId,textId));
    }

    @Override
    public List<PlateGridText> selectListByTextId(Long textId) {
        return plateGridTextMapper.selectList(
                new LambdaQueryWrapper<PlateGridText>()
                        .eq(PlateGridText::getStepTextId,textId));
    }

    @Override
    public List<Long> selectPlateGridIdList(Long plateId) {
        return plateGridTextMapper.selectPlateGridIdList(plateId);
    }

    @Override
    public boolean deleteByExperimentId(Long experimentId) {
        List<Plate> plateList=plateMapper.selectList(
                new LambdaQueryWrapper<Plate>()
                        .eq(Plate::getExperimentId,experimentId)
                        .eq(Plate::getIsDelete,BoolEnum.FALSE.getValue()));
        List<Long> plateIds=plateList.stream().map(Plate::getId).distinct().collect(Collectors.toList());
        if(org.apache.commons.collections.CollectionUtils.isNotEmpty(plateIds)){
            return plateGridTextMapper.delete(
                    new LambdaQueryWrapper<PlateGridText>()
                            .in(PlateGridText::getPlateId,plateIds))>0;
        }
        return true;
    }

    @Override
    public int deleteByStepTextIds(List<Long> textIds) {
        return plateGridTextMapper.delete(
                new LambdaQueryWrapper<PlateGridText>()
                        .in(PlateGridText::getStepTextId,textIds));
    }
}
